package com.jason.basicandroid.ui.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class DialogDemo extends Activity {
	final int DIALOG_ID_YES_NO_SHORT_MSG 	= 1;
	final int DIALOG_ID_YES_NO_LONG_MSG 	= 2;
	final int DIALOG_ID_LIST 				= 3;
	final int DIALOG_ID_PROGRESS 			= 4;
	final int DIALOG_ID_SINGLE_CHOICE 		= 5;
	final int DIALOG_ID_MULTI_CHOICES 		= 6;
	final int DIALOG_ID_TEXT_ENTRY 			= 7;
	
	final CharSequence items[] = {"Red", "Green", "Blue"};
	final boolean[] itemsChoosed = {true, true, false};
	int itemsChoosedIndex = -1;
	
	private ProgressDialog mProgressDialog = null;
	private int mProgressValue = 0;
	private Handler mProgressHandler = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mProgressHandler = new Handler() {
        	@Override
        	public void handleMessage(Message msg) {
        		super.handleMessage(msg);
        		
        		if(mProgressValue >= 100) {
        			mProgressDialog.dismiss();
        		} else {
        			++mProgressValue;
        			mProgressDialog.incrementProgressBy(1);
        			mProgressHandler.sendEmptyMessageDelayed(0, 20);
        		}
        	}
        };
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	Dialog dialog = null;
    	Builder builder = new AlertDialog.Builder(this);
    	
    	switch(id) {
    	case DIALOG_ID_YES_NO_SHORT_MSG:
    		dialog = builder.setCancelable(true)
            	.setIcon(R.drawable.icon)
            	.setTitle("Exit Warning")
    			.setMessage("Are you sure to exit?")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        showPromptDialog("You selected: OK");
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        showPromptDialog("You selected: Cancel");
                    }
                })
                .create();
    		dialog.setOnDismissListener(new OnDismissListener() {
    			public void onDismiss(DialogInterface dia) {
                    showPromptDialog("You dismissed the dialog.");
    			}
    		});
    		break;
    	case DIALOG_ID_YES_NO_LONG_MSG:
    		dialog = builder.setCancelable(false)
        		.setTitle("What is Android?")
        		.setMessage("Android is a software stack for mobile devices that" +
        				"includes an operating system, middleware and key applications." +
        				"The Android SDK provides the tools and APIs necessary to " +
        				"begin developing applications on the Android platform " +
        				"using the Java programming language." +
        				"The following diagram shows the major components of the Android operating system." +
        				"Each section is described in more detail below." +
        				"Android will ship with a set of core applications " +
        				"including an email client, SMS program, calendar, maps, browser, contacts, and others. " +
        				"All applications are written using the Java programming language.")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .create();
    		break;
    	case DIALOG_ID_LIST:
    		dialog = builder.setCancelable(false)
        		.setTitle("Pick a color")
                .setItems(items, new OnClickListener() {
                	public void onClick(DialogInterface dialog, int item) {
                		String str = items[item].toString();
	                    showPromptDialog("You selected: " + str);
	                    dialog.dismiss();
                	}
                })
                .create();
    		break;
    	case DIALOG_ID_PROGRESS:
    		mProgressDialog = new ProgressDialog(this);
    		mProgressDialog.setTitle("Process...");
    		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    		mProgressDialog.setMax(100);
    		mProgressDialog.setButton("Hide", new OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int whichButton) {
    				mProgressDialog.dismiss();
    			}
    		});
    		mProgressDialog.setButton2("Cancel", new OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int whichButton) {
    				mProgressDialog.dismiss();
    			}
    		});
    		dialog = mProgressDialog;
    		
    		break;
    	case DIALOG_ID_SINGLE_CHOICE:
    		dialog = builder.setCancelable(false)
        		.setTitle("Pick a color")
                .setSingleChoiceItems(items, 0, new OnClickListener() {
                	public void onClick(DialogInterface dialog, int item) {
                		itemsChoosedIndex = item;
                	}
                })
        		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                		String str = items[itemsChoosedIndex].toString();
	                    showPromptDialog("You selected: " + str);
	                    dialog.dismiss();
                    }
                })
                .create();
    		break;
    	case DIALOG_ID_MULTI_CHOICES:
    		dialog = builder.setCancelable(false)
    			.setTitle("Pick multi colors")
    			.setMultiChoiceItems(items, itemsChoosed, new OnMultiChoiceClickListener() {
    				public void onClick(DialogInterface dialog, int whichButton, boolean bool) {
    					itemsChoosed[whichButton] = bool;
    				}
    			})
        		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    	String str = "You selected: ";
                    	for(int i = 0; i < items.length; ++i) {
                    		if(itemsChoosed[i]) {
                    			str += items[i] + " ";
                    		}
                    	}
                    	showPromptDialog(str);
                    }
                })
                .create();
    		break;
    	case DIALOG_ID_TEXT_ENTRY:
    		LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.text_entry, null);
            
    		dialog = builder.setCancelable(false)
    			.setTitle("DialogDemo") 
        		.setView(textEntryView) 
        		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    	EditText text = null;
                    	String str = "What you input:\n";
                    	
                    	str += "Username: ";
                    	text = (EditText)textEntryView.findViewById(R.id.txUsername);
                    	str += text.getText().toString();
                    	
                    	str += "\nPassword: ";
                    	text = (EditText)textEntryView.findViewById(R.id.txPassword);
                    	str += text.getText().toString();
                    	
                    	showPromptDialog(str);
                    }
                })
                .create();
    		break;
    	default:
    		dialog = null;
    	}
    	
    	return dialog;
    }
    
    public void onBtnClick(View v) {
    	int id = v.getId();
    	switch(id) {
    	case R.id.btnOKCancelShort:
    		showDialog(DIALOG_ID_YES_NO_SHORT_MSG);
    		break;
    	case R.id.btnOKLong:
    		showDialog(DIALOG_ID_YES_NO_LONG_MSG);
    		break;
    	case R.id.btnList:
    		showDialog(DIALOG_ID_LIST);
    		break;
    	case R.id.btnProgress:
    		showDialog(DIALOG_ID_PROGRESS);
            mProgressValue = 0;
            mProgressDialog.setProgress(0);
            mProgressHandler.sendEmptyMessage(0);
    		break;
    	case R.id.btnSingleChoice:
    		showDialog(DIALOG_ID_SINGLE_CHOICE);
    		break;
    	case R.id.btnMulitChoices:
    		showDialog(DIALOG_ID_MULTI_CHOICES);
    		break;
    	case R.id.btnTextEntry:
    		showDialog(DIALOG_ID_TEXT_ENTRY);
    		break;
    	}
    }
    
    private void showPromptDialog(String str) {
    	new AlertDialog.Builder(DialogDemo.this)
            .setMessage(str)
            .show();
    }
}